package com.hq.fxh.No3_TuiGuang.Fragment;

import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;


import com.hq.fxh.Modle.GetEnterpriseRankRes;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No3_TuiGuang.Adaoter.HeaderEmployeeRankAdapter;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.HttpApi.UserServiceImpl;

import java.util.ArrayList;
import java.util.List;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;
import rx.Subscriber;


/**
 * @author 小谢
 * @ClassName Tg_Qiye_Fragment
 * @Description :企业推广 --企业页面
 * @Created 2016/12/27 0027
 */
public class Tg_Qiye_Fragment extends BaseFragment {
    public RecyclerView recycler;
    private int select = 1;//1曝光 2点击 3转发 4文章
    private int uid;
    private List<GetEnterpriseRankRes.DataBean.UsersBean> dataRank = new ArrayList<>();
    ;
    private HeaderEmployeeRankAdapter adapter;//原BusiPromotionFragmentAdapter
    private int currentPage = 1;
    private int pageSize = 20;
    private int type = 1;//1月榜 2总榜
    private int scrollY;
    private Fragment_Qiye_Kuangjia parentFragment;
    private int height;
    private int SCROLL_LENGTH = 300;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_promotion_busi;
    }

    @Override
    protected void findView(View view) {
        recycler = (RecyclerView) view.findViewById(R.id.recyclerView);
    }

    @Override
    protected void initData() {
        uid = SharedPreferenceUtils.getMemberId(getActivity());

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recycler.setLayoutManager(layoutManager);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);


        adapter = new HeaderEmployeeRankAdapter(getActivity(), dataRank);
        recycler.setAdapter(adapter);
        getEnterpriseRank(true, select, type);
        parentFragment = (Fragment_Qiye_Kuangjia) getParentFragment();
        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {

            private HeaderEmployeeRankAdapter.HeaderViewHolder holder;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                scrollY += dy;
                if (holder == null) {
                    holder = (HeaderEmployeeRankAdapter.HeaderViewHolder) recycler.findViewHolderForAdapterPosition(0);
                }

                if (height == 0) {
                    View mView = adapter.getHeadViewHeight();
                    height = (int) (mView.getHeight() - SCROLL_LENGTH * 1.1);
                }

                if (holder != null) {
                    if (scrollY > height) {
                        parentFragment.setVisible(holder.rl_tl, holder.rl_rg, scrollY - height);

                    } else {
                        parentFragment.setVisible(holder.rl_tl, holder.rl_rg, scrollY - height);

                    }
                }
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            JAnalyticsInterface.onPageStart(getContext(), "推广-企业数据");
        } else {
            JAnalyticsInterface.onPageEnd(getContext(), "推广-企业数据");
        }
    }

    public void getEnterpriseRank(final boolean refresh1, int select, int type) {
        if (refresh1) {
            currentPage = 1;
        } else {
            currentPage++;
        }
        UserServiceImpl.getEnterpriseRank(uid + "", select, type, currentPage, pageSize, new Subscriber<GetEnterpriseRankRes>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                LogUtils.e("请求错误"+e.getMessage());

            }

            @Override
            public void onNext(GetEnterpriseRankRes data) {

                if (data != null && data.getCode() == 0) {
                    if (currentPage == 1) {
                        dataRank.clear();
                        adapter.setCount(data.getData().getTot());
                        if (data.getData().getMyself() == null) {
                            adapter.setmHeaderCount(0);
                        } else {
                            adapter.setmHeaderCount(1);
                            dataRank.add(0, data.getData().getMyself());//data.getData().getMyself(),可能为null
                        }
                    }
                    dataRank.addAll(data.getData().getUsers());

                    adapter.notifyDataSetChanged();

                    adapter.setMyTabSelcctedListener(new HeaderEmployeeRankAdapter.MyTabSelcctedListener() {
                        @Override
                        public void onMyTabSelectedListener(TabLayout.Tab view, int staticsDate) {
                            getEnterpriseRank(true, view.getPosition() + 1, staticsDate);
                        }
                    });
                    adapter.setMyCheckedChangeListener(new HeaderEmployeeRankAdapter.MyCheckedChangeListener() {
                        @Override
                        public void onMyCheckedChangeListener(int staticsType, int staticsDate) {
                            getEnterpriseRank(true, staticsType, staticsDate);
                        }
                    });
                } else {
                    LogUtils.startToash(getContext(), data.getMsg());
                }

            }


        });
    }
}
